package com.op.studyingliteapp.fragment.pptresource;


import java.util.ArrayList;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import android.widget.Toast;
import com.op.studyingliteapp.R;
import com.op.studyingliteapp.fragment.viewer.ViewPagerFragment;
import org.vudroid.pdfdroid.codec.PdfContext;

/**
 * @author handsome
 * @version 创建时间：2015年7月26日 下午11:51:12 类说明
 */
public class PPTResourceFragment extends Fragment {

    private PPTResourceAdapter mAdapter;
    private static final String sdcardPath = Environment
            .getExternalStorageDirectory().getPath();
    public static String Location = sdcardPath + "/DEMO/";
    private View root;

    @SuppressWarnings("unused")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_ppt,
                container, false);
        String url = Environment.getExternalStorageDirectory().getPath();
        String url2 = Environment.getExternalStorageDirectory().getPath();
        ListView mListView = (ListView) root.findViewById(R.id.ppt_listview);
        ArrayList<PPTResourceCellData> ppt_list = new ArrayList<PPTResourceCellData>();
//        if (ppt_list == null) {
            ppt_list.add(new PPTResourceCellData("第1章 Java概述",
                    Location + "java_1.pdf", R.drawable.icon_resource));
            ppt_list.add(new PPTResourceCellData("第2章 Java语言基础", Location + "java_2.pdf", R.drawable.icon_resource));
            ppt_list.add(new PPTResourceCellData("第3章 类的封装、继承和多态", Location + "java_3.pdf", R.drawable.icon_resource));
            ppt_list.add(new PPTResourceCellData("第4章 接口、内部类和包", Location + "java_4.pdf", R.drawable.icon_resource));
            ppt_list.add(new PPTResourceCellData("第5章 异常处理",
                    Location + "java_5.pdf", R.drawable.icon_resource));
            ppt_list.add(new PPTResourceCellData("第6章 图形用户界面",
                    Location + "java_6.pdf", R.drawable.icon_resource));
            ppt_list.add(new PPTResourceCellData("第7章 多线程",
                    Location + "java_7.pdf", R.drawable.icon_resource));
            ppt_list.add(new PPTResourceCellData("云教学",
                    Location + "yunjiaoxue.pdf", R.drawable.icon_resource));
        ppt_list.add(new PPTResourceCellData("Spring Framework 3教程", Location
                + "SpringFramework3.pdf", R.drawable.icon_resource));
//        } else {
//            ppt_list.clear();
//        }
        mAdapter = new PPTResourceAdapter(getActivity(), ppt_list);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
//
                PPTResourceCellData resourceDetailsDataProvider = mAdapter.getItem(position);

                String url = resourceDetailsDataProvider.getUrl();
                if (isPdfOpenable(url)) {
                    ViewPagerFragment pptShow = new ViewPagerFragment();//这个是打开pdf速度快，没有画笔的
                    Bundle bundle = new Bundle();
                    bundle.putString("url", url);
                    pptShow.setArguments(bundle);

                    getFragmentManager().beginTransaction()
                            .addToBackStack(null)
                            .replace(R.id.ppt_show, pptShow).commit();
                } else {
                    Toast.makeText(getActivity(), "该文件无法打开", Toast.LENGTH_SHORT).show();
                }


            }

        });
        return root;

    }

    private boolean isPdfOpenable(String url) {

        PdfContext pdfContext = new PdfContext();
        try {
             pdfContext.openDocument(url);
        } catch (Exception e) {
            return false;
        }


        return true;
    }
}
